package cn.qh.com.controller;

import cn.qh.com.entity.Brand;
import cn.qh.com.entity.Category;
import cn.qh.com.exception.NotFoundException;
import cn.qh.com.service.BrandService;
import cn.qh.com.service.CategoryService;
import cn.qh.com.vo.CommonResponse;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import cn.qh.com.entity.PmsCategoryBrandRelation;
import cn.qh.com.dto.PmsCategoryBrandRelationDTO;
import cn.qh.com.service.PmsCategoryBrandRelationService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import io.swagger.annotations.ApiImplicitParam;
import org.springframework.beans.BeanUtils;

/**
 * 控制层
 * 控制层
 **/
@RestController
@RequestMapping("pmsCategoryBrandRelation")
@Api
public class PmsCategoryBrandRelationController {
    /**
     * 服务对象
     */
    @Resource
    private PmsCategoryBrandRelationService pmsCategoryBrandRelationService;
    @Resource
    private BrandService brandService;
    @Resource
    private CategoryService categoryService;

    /**
     * 分页查询所有数据
     *
     * @param current 分页对象
     * @return 所有数据
     */
    @GetMapping("/list")
    @ApiOperation(value = "分页查询所有数据", produces = "application/json;charset=utf-8",
            httpMethod = "GET", response = CommonResponse.class)
    @ApiImplicitParam(name = "current", value = "当前页", paramType = "query", dataType = "int")
    @CrossOrigin
    public Page<PmsCategoryBrandRelation> selectAll(@RequestParam(defaultValue = "1") int current) {
        Page page = new Page();
        page.setCurrent(current);
        page.setSize(3);
        this.pmsCategoryBrandRelationService.page(page);
        return page;
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    @CrossOrigin
    @ApiOperation(value = "通过主键查询单条数据", produces = "application/json;charset=utf-8",
            httpMethod = "GET", response = CommonResponse.class)
    @ApiImplicitParam(name = "id", value = "主键", paramType = "path", dataType = "int")
    public PmsCategoryBrandRelation selectOne(@PathVariable Serializable id) {
        return this.pmsCategoryBrandRelationService.getById(id);
    }

    /**
     * 新增数据
     *
     * @param pmsCategoryBrandRelationDTO 实体对象
     * @return 新增结果
     */
    @PostMapping("/insert")
    @ApiOperation(value = "新增", produces = "application/json;charset=utf-8",
            httpMethod = "POST", response = CommonResponse.class)
    @CrossOrigin
    public boolean insert(@RequestBody PmsCategoryBrandRelationDTO pmsCategoryBrandRelationDTO) {
        Long brandId = pmsCategoryBrandRelationDTO.getBrandId();
        Long catelogId = pmsCategoryBrandRelationDTO.getCatelogId();
        Brand brandServiceById = brandService.getById(brandId);
        Category categoryServiceById = categoryService.getById(catelogId);
        if (brandServiceById == null) {
            throw new NotFoundException(20001);
        }
        if (categoryServiceById == null) {
            throw new NotFoundException(20002);
        }
        String name = brandServiceById.getName();
        String name1 = categoryServiceById.getName();
        PmsCategoryBrandRelation pmsCategoryBrandRelation = new PmsCategoryBrandRelation();
        BeanUtils.copyProperties(pmsCategoryBrandRelationDTO, pmsCategoryBrandRelation);
        pmsCategoryBrandRelation.setBrandName(name);
        categoryServiceById.setName(name1);
        return this.pmsCategoryBrandRelationService.save(pmsCategoryBrandRelation);
    }

    /**
     * 修改数据
     *
     * @param pmsCategoryBrandRelationDTO 实体对象
     * @return 修改结果
     */
    @PutMapping("/update")
    @ApiOperation(value = "修改信息", produces = "application/json;charset=utf-8",
            httpMethod = "PUT", response = CommonResponse.class)
    @CrossOrigin
    public boolean update(@RequestBody PmsCategoryBrandRelationDTO pmsCategoryBrandRelationDTO) {
        PmsCategoryBrandRelation pmsCategoryBrandRelation = new PmsCategoryBrandRelation();
        BeanUtils.copyProperties(pmsCategoryBrandRelationDTO, pmsCategoryBrandRelation);
        return this.pmsCategoryBrandRelationService.updateById(pmsCategoryBrandRelation);
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除结果
     */
    @DeleteMapping("/del/{id}")
    @ApiOperation(value = "根据主键删除信息", produces = "application/json;charset=utf-8",
            httpMethod = "DELETE", response = CommonResponse.class)
    @ApiImplicitParam(name = "id", value = "主键", paramType = "path", dataType = "int")
    @CrossOrigin
    public boolean delete(@PathVariable Serializable id) {
        return this.pmsCategoryBrandRelationService.removeById(id);
    }
}
